3 * Non-directory file on the file system.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup FileBackend
24 use Wikimedia\AtEase\AtEase
;
27 * Class representing a non-directory file on the file system
29 * @ingroup FileBackend
32 /** @var string Path to file */
35 /** @var string File SHA-1 in base 36 */
36 protected $sha1Base36;
39 * Sets up the file object
41 * @param string $path Path to temporary file on local disk
43 public function __construct( $path ) {
48 * Returns the file system path
52 public function getPath() {
57 * Checks if the file exists
61 public function exists() {
62 return is_file( $this->path
);
66 * Get the file size in bytes
70 public function getSize() {
71 return filesize( $this->path
);
75 * Get the file's last-modified timestamp
77 * @return string|bool TS_MW timestamp or false on failure
79 public function getTimestamp() {
80 AtEase
::suppressWarnings();
81 $timestamp = filemtime( $this->path
);
82 AtEase
::restoreWarnings();
83 if ( $timestamp !== false ) {
84 $timestamp = wfTimestamp( TS_MW
, $timestamp );
91 * Get an associative array containing information about
92 * a file with the given storage path.
94 * Resulting array fields include:
96 * - size (filesize in bytes)
97 * - mime (as major/minor)
98 * - file-mime (as major/minor)
103 * @param string|bool $ext The file extension, or true to extract it from the filename.
104 * Set it to false to ignore the extension. Currently unused.
107 public function getProps( $ext = true ) {
108 $info = self
::placeholderProps();
109 $info['fileExists'] = $this->exists();
111 if ( $info['fileExists'] ) {
112 $info['size'] = $this->getSize(); // bytes
113 $info['sha1'] = $this->getSha1Base36();
115 $mime = mime_content_type( $this->path
);
116 # MIME type according to file contents
117 $info['file-mime'] = ( $mime === false ) ?
'unknown/unknown' : $mime;
119 $info['mime'] = $mime;
121 if ( strpos( $mime, '/' ) !== false ) {
122 list( $info['major_mime'], $info['minor_mime'] ) = explode( '/', $mime, 2 );
124 list( $info['major_mime'], $info['minor_mime'] ) = [ $mime, 'unknown' ];
132 * Placeholder file properties to use for files that don't exist
134 * Resulting array fields include:
136 * - size (filesize in bytes)
137 * - mime (as major/minor)
138 * - file-mime (as major/minor)
139 * - sha1 (in base 36)
145 public static function placeholderProps() {
147 $info['fileExists'] = false;
149 $info['file-mime'] = null;
150 $info['major_mime'] = null;
151 $info['minor_mime'] = null;
152 $info['mime'] = null;
159 * Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case
160 * encoding, zero padded to 31 digits.
162 * 160 log 2 / log 36 = 30.95, so the 160-bit hash fills 31 digits in base 36
165 * @param bool $recache
166 * @return bool|string False on failure
168 public function getSha1Base36( $recache = false ) {
169 if ( $this->sha1Base36
!== null && !$recache ) {
170 return $this->sha1Base36
;
173 AtEase
::suppressWarnings();
174 $this->sha1Base36
= sha1_file( $this->path
);
175 AtEase
::restoreWarnings();
177 if ( $this->sha1Base36
!== false ) {
178 $this->sha1Base36
= Wikimedia\base_convert
( $this->sha1Base36
, 16, 36, 31 );
181 return $this->sha1Base36
;
185 * Get the final file extension from a file system path
187 * @param string $path
190 public static function extensionFromPath( $path ) {
191 $i = strrpos( $path, '.' );
193 return strtolower( $i ?
substr( $path, $i +
1 ) : '' );
197 * Get an associative array containing information about a file in the local filesystem.
199 * @param string $path Absolute local filesystem path
200 * @param string|bool $ext The file extension, or true to extract it from the filename.
201 * Set it to false to ignore the extension.
204 public static function getPropsFromPath( $path, $ext = true ) {
205 $fsFile = new self( $path );
207 return $fsFile->getProps( $ext );
211 * Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case
212 * encoding, zero padded to 31 digits.
214 * 160 log 2 / log 36 = 30.95, so the 160-bit hash fills 31 digits in base 36
217 * @param string $path
218 * @return bool|string False on failure
220 public static function getSha1Base36FromPath( $path ) {
221 $fsFile = new self( $path );
223 return $fsFile->getSha1Base36();